package com.geovis.emergency.spd.biz.resources.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.resources.mapper.BsFxkhzjzMapper;
import com.geovis.emergency.spd.biz.resources.mapper.BsShyjjydwMapper;
import com.geovis.emergency.spd.biz.resources.mapper.BsZhxxdwryMapper;
import com.geovis.emergency.spd.biz.resources.mapper.JzwzcbkdMapper;
import com.geovis.emergency.spd.biz.resources.service.JzwzcbkdService;
import com.geovis.emergency.spd.entity.resources.dto.*;
import com.geovis.emergency.spd.entity.resources.entity.BsShyjjydw;
import com.geovis.emergency.spd.entity.resources.entity.Jzwzcbkd;
import com.geovis.emergency.spd.entity.resources.enums.ResourceTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
@DS("buffer")
public class JzwzcbkdServiceImpl extends ServiceImpl<JzwzcbkdMapper, Jzwzcbkd> implements JzwzcbkdService {


    private final BsShyjjydwMapper bsShyjjydwMapper;

    private final BsFxkhzjzMapper bsFxkhzjzMapper;

    private final BsZhxxdwryMapper bsZhxxdwryMapper;

    @Override
    public List<ResourceListDTO> getJzwzcbkd() {
        List<Jzwzcbkd> jzwzcbkds=this.list();

        return jzwzcbkds.stream().map(jzwzcbkd->{
            ResourceListDTO listDTO=new ResourceListDTO();
            BeanUtils.copyProperties(jzwzcbkd,listDTO);
            listDTO.setTitle(jzwzcbkd.getDwmc())
                    .setTypeName("应急物资仓库");
            listDTO.setLatitude(jzwzcbkd.getLat());
            listDTO.setLongitude(jzwzcbkd.getLon());
            return listDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public JzwzcbkdDetailDTO getJzwzcbkdDetail(String id) {
        Jzwzcbkd jzwzcbkd=this.getById(id);
        JzwzcbkdDetailDTO detailDTO=new JzwzcbkdDetailDTO();
        detailDTO.setAddress(jzwzcbkd.getAddress())
                .setDepotname(jzwzcbkd.getDwmc())
                .setDepotcode(jzwzcbkd.getCode())
                .setLeader(jzwzcbkd.getDwfzr())
                .setLinkman(jzwzcbkd.getDwfzr())
                .setPhone(jzwzcbkd.getLxdh())
                .setUnitname(jzwzcbkd.getRdhzgbm())
                .setBuildtime(jzwzcbkd.getJcsj());

        String goods="";
        if(StringUtils.hasText(jzwzcbkd.getJsy()))
        {
            goods=goods+"救生衣:"+jzwzcbkd.getJsy()+"件;";
        }
        if(StringUtils.hasText(jzwzcbkd.getJsy()))
        {
            goods=goods+"应急灯:"+jzwzcbkd.getYjd()+"件";
        }
        if(StringUtils.hasText(jzwzcbkd.getJsy()))
        {
            goods=goods+"抽水泵:"+jzwzcbkd.getCsb()+"台";
        }
        if(StringUtils.hasText(jzwzcbkd.getJsy()))
        {
            goods=goods+"编织袋:"+jzwzcbkd.getBzd()+"件;";
        }
        detailDTO.setGoods(goods);
        return detailDTO;
    }

    @Override
    public List<ResourceResultDTO> search(ResourceSearchDTO dto) {
        List<ResourceResultDTO> resultDTOS=new ArrayList<>();

        List<Jzwzcbkd> wzcks=new ArrayList<>();
        List<BsShyjjydw> jydws=new ArrayList<>();
        List<PointSearchDTO> zzrs=new ArrayList<>();
        List<PointSearchDTO> zjlls=new ArrayList<>();

        String point="POINT("+dto.getLongitude()+" "+dto.getLatitude()+")";
        Double res= NumberUtil.mul(dto.getDistance(),Double.valueOf("1000.0"));
        Integer distance=res.intValue();
        if(ResourceTypeEnum.WZCK.equals(dto.getTypeCode()))
        {
            wzcks=baseMapper.queryPoints(point,distance);
        }
        if(ResourceTypeEnum.JYDW.equals(dto.getTypeCode()))
        {
            jydws=bsShyjjydwMapper.queryPoints(point,distance);
        }
        if(ResourceTypeEnum.ZJLL.equals(dto.getTypeCode()))
        {
            zjlls=bsFxkhzjzMapper.queryPoints(point,distance);
        }
        if(ResourceTypeEnum.ZZR.equals(dto.getTypeCode()))
        {
            zzrs=bsZhxxdwryMapper.queryPoints(point,distance);
        }


        if(!CollectionUtils.isEmpty(wzcks))
        {
            wzcks.stream().forEach(wzck->{

                ResourceResultDTO result=new ResourceResultDTO();
                result.setId(wzck.getId())
                        .setLatitude(wzck.getLat())
                        .setLongitude(wzck.getLon())
                        .setTitle(wzck.getDwmc())
                        .setTypeCode(ResourceTypeEnum.WZCK);
                resultDTOS.add(result);

            });
        }
        if(!CollectionUtils.isEmpty(jydws))
        {
            jydws.stream().forEach(jydw->{

                ResourceResultDTO result=new ResourceResultDTO();
                BeanUtils.copyProperties(jydw,result);
                result.setTitle(jydw.getTeamName());
                result.setTypeCode(ResourceTypeEnum.JYDW);
                resultDTOS.add(result);

            });

        }
        if(!CollectionUtils.isEmpty(zzrs))
        {
            zzrs.stream().forEach(zzr->{

                ResourceResultDTO result=new ResourceResultDTO();
                BeanUtils.copyProperties(zzr,result);
                result.setTitle(zzr.getName())
                        .setTypeCode(ResourceTypeEnum.ZZR);
                resultDTOS.add(result);

            });
        }
        if(!CollectionUtils.isEmpty(zjlls))
        {
            zjlls.stream().forEach(zjll->{

                ResourceResultDTO result=new ResourceResultDTO();
                BeanUtils.copyProperties(zjll,result);
                result.setTitle(zjll.getName())
                        .setTypeCode(ResourceTypeEnum.ZJLL);
                resultDTOS.add(result);

            });
        }


        return resultDTOS;
    }
}
